package com.yu.serviceImpl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.common.excel.ExcelCommon;
import com.yu.config.result.BackResult;
import com.yu.domain.loginUser.dto.User;
import com.yu.service.ExcelService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.compress.utils.CharsetNames;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.HttpHeaders;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ExcelServiceImpl extends ServiceImpl implements ExcelService {


    /**
     * 第一种导入  实体类的标签是 @ExcelProperty(value = "*字典")  只能一对一的导入
     *
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public BackResult importOneExcel(MultipartFile file) throws IOException {
//        ExcelListener excelListener = new ExcelListener(this);
//        //3个参数  1文件输入流  2是导入的excel的实体类  3是监听类
//        EasyExcel.read(file.getInputStream(), EnumModelExcelParam.class, excelListener).sheet().headRowNumber(1).doRead();

        return null;
    }

    /**
     * 第二种导入  实体类的标签是@Excel   可以一对一，一对多的导入
     *
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public BackResult importTwoExcel(MultipartFile file) throws IOException {
//        ImportParams importParams = new ImportParams();
//        importParams.setTitleRows(0);
//        importParams.setHeadRows(2);
//        importParams.setSheetNum(1);
//        //importExcel  3个参数 1是输入流  2是excel导出的类   3是从哪开始
//        List<DictModelExcelParam> objects = ExcelImportUtil.importExcel(file.getInputStream(), DictModelExcelParam.class, importParams);
        return null;
    }

    /**
     * 第一种导出方法  一对一
     * @param response
     * @param file
     * @throws IOException
     */
    @Override
    public void exportOneExcel(HttpServletResponse response, MultipartFile file) throws IOException {
        //从数据库中查取的数据
        List<User> list = new ArrayList();
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = ExcelCommon.downLoadExcelHeader(response);
        String fileName = "枚举_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());

        response.setContentType("application/msexcel");
        fileName = URLEncoder.encode(fileName, CharsetNames.UTF_8) + ".xlsx";
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);

        EasyExcel.write(response.getOutputStream(), User.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("枚举")
                .registerWriteHandler(horizontalCellStyleStrategy)
                .doWrite(list);
    }


    /**
     * 第二种导出方法  一对多  一对一
     * @param response
     * @throws IOException
     */
    @Override
    public void exportTwoExcel(HttpServletResponse response) throws IOException {
        ExportParams exportParams = new ExportParams();
        //从数据库中查出的数据   一对多的话数据需要有多个查询语句，先查出一个出结果，然后已第一个的结果做第二个的查询参数
        // <result><collection property="menuList" column="id=id" javaType="java.util.List"
        // select="menuPage" ofType="com.yu.domain.excel.MenuExcelVo"></collection></result>
        List<User> list = new ArrayList();
        //3个参数  1是输出流 2是导出的excel的实体类  3 查出的数据库数据
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, User.class, list);
        String fileName = "字典_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()) + ".xlsx";
        ExcelCommon.downLoadExcelTail(fileName, response, sheets);
    }
}
